package com.lin.blog.controller;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.blog.entity.Article;
import com.lin.blog.service.IArticleService;
import com.lin.constant.ResultCode;
import com.lin.mq.MQConstant;
import com.lin.mq.MQUtils;
import com.lin.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lin
 * @since 2020-03-08
 */
@Api(tags = "Article接口")
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private IArticleService articleService;

    @Autowired
    private MQUtils mqUtils;


    /**
     * 通过主键ID查询ArticleContent
     * @param id
     * @return
     */
    @ApiOperation("通过主键ID查询ArticleContent")
    @GetMapping("/content/{id}")
    public Result getArticleContent(@PathVariable("id") Long id)
    {
        return articleService.getArticleContent(id);
    }


    /**
     * 获取ArticleList时间线
     * @return
     */
    @ApiOperation("获取ArticleList时间线")
    @GetMapping("/timeLine")
    public Result getArticleTimeLine(){
        return articleService.getArticleTimeLine();
    }

    /**
     * 增加文章浏览
     * @return
     */
    @ApiOperation("增加文章浏览")
    @PutMapping("/looks/{articleId}")
    public Result incrementArticleLooks(@PathVariable("articleId") Long articleId){
        return articleService.incrementArticleLooks(articleId);
    }


    /**
     * 插入一条Article记录
     * @param article
     * @return
     */
    @RequiresPermissions("article:add")
    @ApiOperation("插入一条Article记录")
    @PostMapping
    public Result insertArticle(@RequestBody @Valid Article article) throws Exception {
        Result result = articleService.saveArticle(article);
        if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
            // 发送MQ消息
            mqUtils.sendMQMessage(article.getId(), MQConstant.TOPIC_ARTICLE_INSERT);
        }
        return result;
    }


    /**
     * 通过主键ID删除Article
     * @param id
     * @return
     */
    @RequiresPermissions("article:delete")
    @ApiOperation("通过主键ID删除Article")
    @ApiImplicitParam(name = "id", value = "ID", defaultValue = "0",required = true)
    @DeleteMapping("/{id}")
    public Result deleteArticleById(@PathVariable(value = "id",required = true) Long id) throws Exception {
        Result result = articleService.removeArticle(id);
        if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
            // 发送MQ消息
            mqUtils.sendMQMessage(id, MQConstant.TOPIC_ARTICLE_REMOVE);
        }
        return result;
    }

    /**
     * 通过主键ID更新Article
     * @param id
     * @param article
     * @return
     */
    @RequiresPermissions("article:update")
    @ApiOperation("通过主键ID更新Article")
    @ApiImplicitParam(name = "id", value = "ID", defaultValue = "0",required = true)
    @PutMapping("/{id}")
    public Result updateArticle(@PathVariable(value = "id",required = true) Long id,@RequestBody Article article) throws Exception {
        Result result = articleService.updateArticle(id, article);
        if (ResultCode.SUCCESS.getCode().equals(result.getCode())) {
            // 发送MQ消息
            mqUtils.sendMQMessage(id, MQConstant.TOPIC_ARTICLE_UPDATE);
        }
        return result;
    }







}
